package com.example.demo_day02.service.impl;

import com.example.demo_day02.mapper.AutomobileMapper;
import com.example.demo_day02.pojo.TbAutomobile;
import com.example.demo_day02.service.AutomobileService;
import com.example.demo_day02.utils.AppHttpCodeEnum;
import com.example.demo_day02.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class AutomobileServiceImpl implements AutomobileService {

    @Autowired
    private AutomobileMapper automobileMapper;


    @Override
    public ResponseResult automobile_finAll() {
        List<TbAutomobile> automobiles = automobileMapper.selectList(null);
        log.info("显示全部:{}",automobiles);
        return ResponseResult.okResult(automobiles);
    }

    @Override
    public ResponseResult update_automobile(TbAutomobile tbAutomobile) {
        if (tbAutomobile == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (automobileMapper.selectById(tbAutomobile.getId()) == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        log.info("修改:{}",tbAutomobile);
        automobileMapper.updateById(tbAutomobile);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete_automobile(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (automobileMapper.selectById(id) == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        log.info("删除id为:{}",id);
        automobileMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult add_automobile(TbAutomobile tbAutomobile) {
        if (tbAutomobile == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        log.info("添加:{}",tbAutomobile);
        automobileMapper.insert(tbAutomobile);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult automobile_byId(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (automobileMapper.selectById(id) == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        TbAutomobile automobileById = automobileMapper.selectById(id);
        log.info("回显:{}",automobileById);
        return ResponseResult.okResult(automobileById);
    }

}
